package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsComment;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/23 15:24
 * description:
 */
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {


    @Autowired
    DtsCommentService commentService;

    @Autowired
    DtsBrandService brandService;

    @RequiresPermissions("admin:comment:list")
    @RequiresPermissionsDesc(menu = { "用户管理", "商品评论" }, button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit",required = false,defaultValue = "10") Integer limit,
            String sort,String order, Integer userId,@RequestParam(name ="valueId",required = false) Integer goodsId
    ){
        //判断 当前登录用户是否是品牌制造商，如果是，只查询品牌商品的评论数据
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds=null;
        if(subject.hasRole("品牌制造商")){
            //查询品牌id
             brandIds= brandService.queryByAdminId(admin.getId());
        }
        IPage<DtsComment> ipage=commentService.queryPage(page,limit,sort,order,userId,goodsId,brandIds);
        Map<String,Object> data=new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:comment:delete")
    @RequiresPermissionsDesc(menu = { "用户管理", "商品评论" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsComment comment){
        commentService.delete(comment);
        return ResponseUtil.ok();
    }



}
